<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="批准文号">
              <a-input placeholder="请输入批准文号" v-model="queryParam.yid"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="药品名称">
              <a-input placeholder="请输入药品名称" @blur="ynamefun" v-model="queryParams.yname"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-item label="药品类别">
                <j-dict-select-tag v-model="queryParam.ylid" placeholder="请选择药品类别"
                                   dictCode="medicine_cate,ylname,ylid"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="规格">
                <a-input placeholder="请输入规格" v-model="queryParam.yspecifications"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="16">
              <a-form-item label="参考进价">
                <a-input-number placeholder="请输入最小值" @blur="yppricefun" class="query-group-cust"
                                v-model="queryParam.ypprice_begin"></a-input-number>
                <span class="query-group-split-cust"></span>
                <a-input-number placeholder="请输入最大值" @blur="yppricefun" class="query-group-cust"
                                v-model="queryParam.ypprice_end"></a-input-number>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="16">
              <a-form-item label="参考售价">
                <a-input-number placeholder="请输入最小值" @blur="yspricefun" class="query-group-cust"
                                v-model="queryParam.ysprice_begin"></a-input-number>
                <span class="query-group-split-cust"></span>
                <a-input-number placeholder="请输入最大值" @blur="yspricefun" class="query-group-cust"
                                v-model="queryParam.ysprice_end"></a-input-number>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="16">
              <a-form-item label="供应商名称">
                <a-input placeholder="请输入供应商名称" @blur="gysnamefun" v-model="queryParams.gysname"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('药品信息表')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
    </div>

    <!-- table区域-begin -->
    <div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">

        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <img :src="getAvatarView(record.avatar)" @click="handlePreview(record.avatar)" alt="" style="height:104px;max-width:300px"/>
          </div>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleaddBuyer(record)">进货</a>
          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleEdit(record)">编辑</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>

      <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
        <img alt="example" style="width: 100%" :src="previewImage" />
      </a-modal>

    </div>

    <medicineInfo-modal ref="modalForm" @ok="modalFormOk"></medicineInfo-modal>
    <MedicineBuyerAddModal ref="modalForm1" @ok="modalFormOk"></MedicineBuyerAddModal>
  </a-card>
</template>

<script>

    import {JeecgListMixin} from '@/mixins/JeecgListMixin'
    import MedicineInfoModal from './modules/MedicineInfoModal'
    import MedicineBuyerAddModal from './modules/MedicineBuyerAddModal'
    import JDate from '@/components/jeecg/JDate.vue'
    import {initDictOptions, filterDictText} from '@/components/dict/JDictSelectUtil'
    import {axios} from '@/utils/request'

    export default {
        name: "MedicineInfoList",
        mixins: [JeecgListMixin],
        components: {
            JDate,
            MedicineInfoModal,
            MedicineBuyerAddModal
        },
        data() {
            return {
                description: '药品信息表管理页面',
                cateDictOptions: [],
                // 表头
                columns: [
                    {
                        title: '#',
                        dataIndex: '',
                        key: 'rowIndex',
                        width: 60,
                        align: "center",
                        customRender: function (t, r, index) {
                            return parseInt(index) + 1;
                        }
                    },
                    {
                        title: '批准文号',
                        align: "center",
                        dataIndex: 'yid'
                    },
                    {
                        title: '药品名称',
                        align: "center",
                        dataIndex: 'yname'
                    },
                    {
                        title: '药品图片',
                        align: "center",
                        width: 120,
                        dataIndex: 'avatar',
                        scopedSlots: {customRender: "avatarslot"}
                    },
                    {
                        title: '药品类别',
                        align: "center",
                        dataIndex: 'ylid',
                        customRender: (text, record, index) => {
                            //字典值替换通用方法
                            return filterDictText(this.cateDictOptions, `${text}`);
                        }
                    },
                    {
                        title: '参考进价',
                        align: "center",
                        dataIndex: 'ypprice'
                    },
                    {
                        title: '参考售价',
                        align: "center",
                        dataIndex: 'ysprice'
                    },
                    {
                        title: '规格',
                        align: "center",
                        dataIndex: 'yspecifications'
                    },
                    {
                        title: '供应商',
                        align: "center",
                        dataIndex: 'gysname'
                    },
                    {
                        title: '备注',
                        align: "center",
                        dataIndex: 'yremarks'
                    },
                    {
                        title: '操作',
                        dataIndex: 'action',
                        align: "center",
                        scopedSlots: {customRender: 'action'}
                    }
                ],
                url: {
                    imgerver: window._CONFIG['domianURL'] + "/sys/common/view",
                    list: "/medicine/medicineInfo/list",
                    delete: "/medicine/medicineInfo/delete",
                    deleteBatch: "/medicine/medicineInfo/deleteBatch",
                    exportXlsUrl: "/medicine/medicineInfo/exportXls",
                    importExcelUrl: "medicine/medicineInfo/importExcel",
                },
                dictOptions: {},
                data: [],
                previewVisible: false,
                previewImage: '',
            }
        },
        computed: {
            importExcelUrl: function () {
                return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
            }
        },
        created() {
            //页面加载函数
            this.initDictConfig();
        },
        methods: {
            initDictConfig() {
            },
            ynamefun() {
                this.queryParam.yname = '*' + this.queryParams.yname + '*';
            },
            gysnamefun() {
                this.queryParam.gysname = '*' + this.queryParams.gysname + '*';
            },
            yppricefun() {
                if (this.queryParam.ypprice_begin != '' && this.queryParam.ypprice_end != '') {
                    if (this.queryParam.ypprice_begin > this.queryParam.ypprice_end) {
                        this.$message.error('进价最小值必须小于最大值');
                        this.queryParam.ypprice_begin = '';
                        this.queryParam.ypprice_end = '';
                    }
                }
            },
            yspricefun() {
                if (this.queryParam.ysprice_begin != '' && this.queryParam.ysprice_end != '') {
                    if (this.queryParam.ysprice_begin > this.queryParam.ysprice_end) {
                        this.$message.error('售价最小值必须小于最大值');
                        this.queryParam.ysprice_begin = '';
                        this.queryParam.ysprice_end = '';
                    }
                }
            },
            handleaddBuyer(record) {
                this.$refs.modalForm1.add(record);
                this.$refs.modalForm1.title = "进货";
                this.$refs.modalForm1.disableSubmit = false;
            },
            initDictConfig() {
                //初始化字典 - 性别
                initDictOptions('cate').then((res) => {
                    if (res.success) {
                        this.cateDictOptions = res.result;
                    }
                });
            },
            getAvatarView: function (avatar) {
                return this.url.imgerver + "/" + avatar;
            },
            handlePreview(avatar){
                this.previewImage=this.url.imgerver + "/" + avatar;
                this.previewVisible = true;
            },
            handleCancel() {
                this.previewVisible = false;
            },
        }
    }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>